//
//  ZWTagListView.h
//  自定义流式标签
//
//  Created by zhangwei on 15/10/22.
//  Copyright (c) 2015年 zhangwei. All rights reserved.
//

#import "ZWTagListView.h"
#import "CRGradientLabel.h"
#import <YYKit/YYKit.h>

#define HORIZONTAL_PADDING 1.0f
#define VERTICAL_PADDING   3.0f
#define LABEL_MARGIN       10.0f
#define BOTTOM_MARGIN      5.0f
@implementation ZWTagListView
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        totalHeight=0;
        self.frame=frame;
    }
    return self;
}
-(void)setTagWithTagArray:(NSArray*)arr{
    
    previousFrame = CGRectZero;
    [arr enumerateObjectsUsingBlock:^(NSString*str, NSUInteger idx, BOOL *stop) {
        CRGradientLabel*tag=[[CRGradientLabel alloc]initWithFrame:CGRectZero];
        if(_signalTagColor){
            //可以单一设置tag的颜色
            tag.backgroundColor=_signalTagColor;
        }else{
            //tag颜色多样
//            tag.backgroundColor=[UIColor colorWithRed:random()%255/255.0 green:random()%255/255.0 blue:random()%255/255.0 alpha:1];
//            switch (idx) {
//                case 0:
//                    tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#FF679E"].CGColor,(id)[UIColor colorWithHexString:@"#FFB698"].CGColor, nil];
//
//                    break;
//                case 1:
//                    tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#FE9F46"].CGColor,(id)[UIColor colorWithHexString:@"#FECF59"].CGColor, nil];
//
//                    break;
//
//                case 2:
//                    tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#B184FE"].CGColor,(id)[UIColor colorWithHexString:@"#89CEFE"].CGColor, nil];
//
//                    break;
//
//                default:
//                    tag.backgroundColor=[UIColor colorWithRed:random()%255/255.0 green:random()%255/255.0 blue:random()%255/255.0 alpha:1];
//
//                    break;
//            }
            if ([str isEqualToString:@"达人"]) {
                tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#B184FE"].CGColor,(id)[UIColor colorWithHexString:@"#89CEFE"].CGColor, nil];
            }else if ([str isEqualToString:@"认证车主"]){
                tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#FF679E"].CGColor,(id)[UIColor colorWithHexString:@"#FFB698"].CGColor, nil];
            }else {
                tag.gradientColors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#FE9F46"].CGColor,(id)[UIColor colorWithHexString:@"#FECF59"].CGColor, nil];
            }
            
        }
        tag.textAlignment=NSTextAlignmentCenter;
        tag.textColor=[UIColor whiteColor];
        tag.font=[UIFont boldSystemFontOfSize:10];
        tag.text=str;
        tag.layer.cornerRadius=5;
        tag.clipsToBounds=YES;
        NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:14]};
        CGSize Size_str=[str sizeWithAttributes:attrs];
        Size_str.width += HORIZONTAL_PADDING*2;
        Size_str.height += VERTICAL_PADDING*2;
        CGRect newRect = CGRectZero;
        CGFloat x;
        if (str.length == 2) {
            x = 38;
        }else {
            x = 10;
        }
        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
            newRect.origin = CGPointMake(x, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
            totalHeight +=Size_str.height + BOTTOM_MARGIN;
        }
        else {
            if (str.length  == 2 && !self.detailPage) {
                newRect.origin = CGPointMake(28 + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);

            }else {
                newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);

            }
        }
//        if (previousFrame.origin.x + previousFrame.size.width + Size_str.width + LABEL_MARGIN > self.bounds.size.width) {
//            newRect.origin = CGPointMake(10, previousFrame.origin.y + Size_str.height + BOTTOM_MARGIN);
//            totalHeight +=Size_str.height + BOTTOM_MARGIN;
//        }
//        else {
//            newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
//        }
        newRect.size = Size_str;
        [tag setFrame:newRect];
        
        
        previousFrame=tag.frame;
        [self setHight:self andHight:totalHeight+Size_str.height + BOTTOM_MARGIN];
        
//        CAGradientLayer *layer = [CAGradientLayer layer];
//           layer.startPoint = CGPointMake(0, 0);//（0，0）表示从左上角开始变化。默认值是(0.5,0.0)表示从x轴为中间，y为顶端的开始变化
//           layer.endPoint = CGPointMake(1, 0);//（1，1）表示到右下角变化结束。默认值是(0.5,1.0)  表示从x轴为中间，y为低端的结束变化
//        layer.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithHexString:@"#FFB698"].CGColor,(id)[UIColor colorWithHexString:@"#FF679E"].CGColor, nil];
//       layer.frame = tag.layer.bounds;
//           [tag.layer insertSublayer:layer atIndex:0];
//        layer.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor clearColor]);
        [self addSubview:tag];
    }
     ];
    if(_GBbackgroundColor){
        self.backgroundColor=_GBbackgroundColor;
    }else{
        self.backgroundColor=[UIColor clearColor];
    }
}
#pragma mark-改变控件高度
- (void)setHight:(UIView *)view andHight:(CGFloat)hight
{
    CGRect tempFrame = view.frame;
    tempFrame.size.height = hight;
    view.frame = tempFrame;
}
@end
